<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/9/8
 * Time: 9:41
 * QQ:1467572213
 */

namespace app\agent\model;

use think\Model;
use think\Db;
use think\facade\Config;

class Withdrawal extends Model
{
    public static function index($agent_id,$keyword)
    {
        $where['pid'] = $agent_id;
        $where['type'] = 2;
        if ($keyword){
            $list = Db::name('fd_withdrawal')
                ->where($where)
                ->where('username',$keyword)
                ->order('id','desc')
                ->paginate(10,false,['query'=>request()->param()]);
            return $list;
        }else{
            $list = Db::name('fd_withdrawal')
                ->where($where)
                ->order('id','desc')
                ->paginate(10,false,['query'=>request()->param()]);
            return $list;
        }

    }

    public static function withdrawal_pass($agent_id,$id)
    {
        $where['id'] = $id;
        $where['pid'] = $agent_id;
        $where['agent_check'] = 0;
        $where['type'] = 2;
        $pass = Db::name('fd_withdrawal')->where($where)->setField('agent_check',1);
        if ($pass == true){
            return 'success';
        }else{
            return 'error';
        }
    }

    public static function withdrawal_refuse($agent_id,$id)
    {
        $where['id'] = $id;
        $where['pid'] = $agent_id;
        $where['agent_check'] = 0;
        $where['type'] = 2;
        $refuse_info = Db::name('fd_withdrawal')->where($where)->find();
        if ($refuse_info){
            $refuse = Db::name('fd_withdrawal')->where($where)->setField('agent_check',2);
            if ($refuse == true){
                $inc_money = Db::name('fd_user')->where('id',$refuse_info['uid'])->setInc('balance',$refuse_info['apply_money']);
                if ($inc_money == true){
                    return 'success';
                }else{
                    return 'error_money';
                }
            }else{
                return 'error_status';
            }
        }else{
            return 'error_info';
        }
    }


}